package com.javabasestage2.Object.ObjectMiddle.equals;

import java.util.Objects;

public class equalsExercise {
    public static void main(String[] args) {
        Person person = new Person("ff", 18, "男");
        final Person person1 = new Person("ff", 18, "男");
        System.out.println(person.equals(person1));//没重写equals方法,又是Person类指不同对象，内存地址不同，不相等，为假
        //重写equals和hashcode以后就变成真了



    }



}

class  Person{
    private String name;
    private int age;
    private String gender;

    @Override
    public boolean equals(Object o) { // 接收person1
        //调用方法时Person对象指向谁,this就是谁(perosn)      o(perosn1)
        if (this == o) {//判断两个对象是否为同一个对象 ,这边perosn和person1不是同一个对象，所以执行下一下条 Person是Object的子类
            return true;
        }
        //类型判断
        if(o instanceof  Person){ //o 是 Person类或子类，我们才比较
            Person p = (Person) o;//向下转型,o->Person, 因为我们需要得到o的 各种属性
            return this.name.equals(p.name) && this.age == p.age && this.gender.equals(p.gender);//String用equals，int用 ==
                    //this.name 就是 person   (o)p.name 就是 person1  ,,
        }

        //如果不是Perosn或它的子类则直接返回false
        return false;//因为本身是布尔型
//        Person person = (Person) o;//向下转型
//        return age == person.age && Objects.equals(name, person.name) && Objects.equals(gender, person.gender);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, gender);
    }

    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}